package com.ctyun.xstore.s3.controller;

import com.ctyun.xstore.s3.api.object.GeneratePresignedPutUrl;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/s3")
public class S3DemoController {
    @RequestMapping(value = "/presignRequest")
    public List<Map<String, String>> presignRequest(@RequestParam(value = "file", required = true) String fileName, int fileSize, int partSize) throws IOException {
        String bucketName = "vod-origin-60904cdc-c9fe-4a64-9386-7803f9bff21f";
        int blockSize = fileSize / partSize;
        blockSize = fileSize % partSize == 0 ? blockSize : blockSize + 1;
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 1; i <= blockSize; i++) {
            Long milliSecond = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            String keyName = i + "-" + milliSecond + "-" + fileName;
            Map<String, String> map = new HashMap<>();
            map.put("key", keyName);
            map.put("url", GeneratePresignedPutUrl.generatePresignedPutUrl(keyName, bucketName));
            list.add(map);
        }
        return list;
    }
}
